package com.gimi.cloud.bbp.app.consumer.service.impl;

import com.gimi.cloud.bbp.app.consumer.feign.UserFeignService;
import com.gimi.cloud.bbp.app.consumer.service.UserService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.IPUtil;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.NumberUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.entity.user.User;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User service实现类
 * create by xiankejin on 2017-10-13
 */
@Service
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    private UserFeignService userFeignService;

    @Autowired
    UserServiceImpl(UserFeignService userFeignService) {
        this.userFeignService = userFeignService;
    }

    @Override
    public void getUserRegisterCode(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("srcData:" + requestData);
        User user = JsonUtil.jsonToBean(requestData, User.class);
        //验证参数合法性
        if (user == null || user.getMobilePhone() == null) {
            logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Pattern p = Pattern.compile(ServiceNameConstants.PHONE_FORMAT);
        Matcher m = p.matcher(user.getMobilePhone());
        //验证手机号码格式
        if (!m.matches()) {
            logger.error("手机号格式错误");
            throw new MsgException(ResponseCodeEnum.ERR_PHONE_INVALID);
        }
        Util.sendResponse(userFeignService.getUserRegisterCode(user), httpServletResponse);
    }

    @Override
    public void applicationFormRetrieveInfo(String requestData, HttpServletResponse response, HttpServletRequest request) throws MsgException {
        String userId = request.getHeader("userId");
        if(StringUtil.isBlank(userId)){
            throw new MsgException(ResponseCodeEnum.ERR_USER_INVALID);
        }
        User user = new User();
        try {
            user.setUserId(NumberUtil.getInt(userId));
        } catch (NumberFormatException e) {
            throw new MsgException(ResponseCodeEnum.ERR_USER_INVALID);
        }
        Util.sendResponse(userFeignService.applicationFormRetrieveInfo(user), response);
    }

    @Override
    public void getCarrierDataSubmitNoteCode(String requestData, HttpServletResponse response, HttpServletRequest request) throws MsgException {
        String userId = request.getHeader("userId");
        if (!NumberUtils.isCreatable(userId)) {
            throw new MsgException(ResponseCodeEnum.ERR_USER_INVALID);
        }
        Map<String, Object> paramMap = JsonUtil.jsonToMapObject(requestData);
        paramMap.put("ip", IPUtil.getRemortIP(request));
        try {
            paramMap.put("userid", Long.parseLong(userId));
        } catch (NumberFormatException e) {
            throw new MsgException(ResponseCodeEnum.ERR_USER_INVALID);
        }
        Util.sendResponse(userFeignService.getCarrierDataSubmitNoteCode(paramMap), response);
    }

    @Override
    public void getInitpasswordPwdCode(String requestData, HttpServletResponse response, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        User user = JsonUtil.jsonToBean(requestData, User.class);
        //验证参数合法性
        if (user == null || user.getMobilePhone() == null) {
            logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }

        Util.sendResponse(userFeignService.getInitpasswordPwdCode(user), response);
    }

    @Override
    public void getModifypasswordCode(String requestData, HttpServletResponse response, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        User user = JsonUtil.jsonToBean(requestData, User.class);
        //验证参数合法性
        if (user == null || user.getMobilePhone() == null) {
            logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Pattern p = Pattern.compile(ServiceNameConstants.PHONE_FORMAT);
        Matcher m = p.matcher(user.getMobilePhone());
        //验证手机号码格式
        if (!m.matches()) {
            logger.error("手机号格式错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(userFeignService.getModifypasswordCode(user), response);
    }

}
